package com.systemCapacity.controller;

import com.systemCapacity.common.dto.BattlePlanDto;
import com.systemCapacity.common.lang.Result;
import com.systemCapacity.service.BattlePlanService;
import com.systemCapacity.service.BattleTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/plan")
public class BattlePlanController {

    @Autowired
    BattleTaskService battleTaskService;

    @Autowired
    BattlePlanService battlePlanService;

    /**
     * 查询作战方案
     * @param taskId 作战任务
     * @return 作战方案
     */
    @GetMapping("/list")
    public Result getPlans(@RequestParam(value = "taskId") String taskId) {
        if (!battleTaskService.taskExist(taskId)) {
            return Result.fail("作战任务不能为空");
        }
        return Result.succ(battlePlanService.getBattlePlans(taskId));
    }

    /**
     * 查询作战方案
     * @param taskId 作战任务
     * @return 作战方案
     */
    @GetMapping("/listPlan")
    public Result getPlansNew(@RequestParam(value = "taskId") String taskId) {
        if(!StringUtils.isEmpty(taskId)){
            return Result.succ(battlePlanService.getBattlePlansNew(taskId));
        }else{
            return Result.succ(battlePlanService.getRecommendPlans());
        }
    }

    /**
     * 保存作战方案
     * @param dto 作战方案
     * @return 保存结果
     */
    @PostMapping("/save")
    public Result savePlan(@RequestBody @Validated BattlePlanDto dto) {
        if(!StringUtils.isEmpty(dto.getTaskId())){
            battlePlanService.saveBattlePlanNew(dto,false,false);
        }else{
            battlePlanService.saveRecommendPlan(dto);
        }
        return Result.succ("保存成功");
    }

    /**
     * 删除作战方案
     * @param planId 作战方案id
     * @return 删除结果
     */
    @GetMapping("/delete")
    public Result deletePlan(@RequestParam(value =  "planId") String planId) {
        if (!battlePlanService.planExists(planId)) {
            return Result.fail("删除作战方案不存在");
        }
        if (battlePlanService.deletePlanNew(planId)) {
            return Result.succ(null);
        }
        return Result.fail("删除失败，请稍后再试");
    }

    /**
     * 先删除作战方案，再添加
     * @param dto 作战方案
     * @return 操作结果
     */
    @PostMapping("/edit")
    public Result editBattlePlan(@RequestBody BattlePlanDto dto) {
        if (!battlePlanService.planExists(dto.getPlanId())) {
            return Result.fail("删除作战方案不存在");
        }
        battlePlanService.editPlan(dto);
        return Result.succ(null);
    }

    /**
     * 查询作战方案
     * @param planId 作战方案id
     * @return 查询结果
     */
    @GetMapping("/{planId}")
    public Result getBattlePlan(@PathVariable(value = "planId") String planId) {
        if (!battlePlanService.planExists(planId)) {
            return Result.fail("作战方案不存在");
        }
//        return Result.succ(battlePlanService.getBattlePlan(planId));
        return Result.succ(battlePlanService.getBattlePlansDetail(planId));
    }

    /**
     * 作战方案推荐
     * @param planId 作战方案id
     * @return 查询结果
     */
    @GetMapping("/recommendPlan")
    public Result getRecommendPlan(String planId) {
        return Result.succ(battlePlanService.getRecommendPlan(planId));
    }

    /**
     * 作战方案推荐
     * @param taskId 任务id
     * @return 查询结果
     */
    @GetMapping("/saveRecommendPlan")
    public Result saveRecommendPlan(String taskId) {
        Result result = null;
        try {
            result = battlePlanService.saveRecommendPlan(taskId);
        } catch (Exception e) {
            result = Result.fail("操作失败");
        }
        return result;
    }
}
